package com.runtime.hongyunmanage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runtime.hongyunmanage.domain.Carousels;
import com.runtime.hongyunmanage.domain.vo.OrdersVo;
import com.runtime.hongyunmanage.mapper.CarouselsMapper;
import com.runtime.hongyunmanage.service.CarouselsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class CarouselsServiceimpl  extends ServiceImpl<CarouselsMapper, Carousels> implements CarouselsService {
    @Autowired
    private CarouselsMapper carouselsMapper;


    @Override
    public List<Carousels> findAll() {
        return carouselsMapper.findAll();
    }

    @Transactional
    @Override
    public void swapSortOrder(Integer upId, Integer downId) {
        Carousels upCarousel = carouselsMapper.selectById(upId);
        Carousels downCarousel = carouselsMapper.selectById(downId);
// 提取排序值
        Integer upSortOrder = upCarousel.getSortOrder();
        Integer downSortOrder = downCarousel.getSortOrder();

// 调用更新方法（传递四个参数）
        carouselsMapper.swapCarouselSortOrder(upId, downId, upSortOrder, downSortOrder);
    }

    @Override
    public int delete(int Id) {
        return carouselsMapper.delete(Id);
    }

    @Override
    public int addImageByType(String filePath, String description, String endDate) {
        // 获取当前最大的 sortOrder 值
        Integer maxSortOrder = carouselsMapper.findMaxSortOrder();
        int newSortOrder = (maxSortOrder != null) ? maxSortOrder + 1 : 1; // 如果是第一个记录，从1开始
        // 插入新记录，使用新的 sortOrder 值
        return carouselsMapper.addImageByType(filePath, description, newSortOrder,endDate, new Date(), 1, "");
    }

    @Override
    public Integer updateDetail(Integer id, String imageUrl, String description, String endDate) {
        return carouselsMapper.updateDetail(id, imageUrl, description, endDate);
    }

    @Override
    public Carousels selectId(int Id) {
        return carouselsMapper.selectId(Id);
    }
    //666
}
